package com.lanou.zy.projecta.database;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

import com.lanou.zy.projecta.application.BaseApplication;
import com.lanou.zy.projecta.greedao.entity.DaoSession;
import com.lanou.zy.projecta.greedao.entity.HotNotes;
import com.lanou.zy.projecta.greedao.entity.SlideResult;
import com.lanou.zy.projecta.greedao.entity.SubjectResult;
import com.lanou.zy.projecta.greedao.entity.User;
import com.lanou.zy.projecta.greedao.entity.UserDao;

import java.util.List;


import javax.security.auth.Subject;

import de.greenrobot.dao.query.DeleteQuery;
import de.greenrobot.dao.query.QueryBuilder;

/**
 * Created by dllo on 15/11/8.
 */
public class DBHelper {
    private static DBHelper helper;
    private static DaoSession daoSession;

    /**
     * 重写构造方法为私有的  不给外部调用
     * */
    private DBHelper() {

    }

    public static DBHelper getInstance() {
        if (helper == null) {
            helper = new DBHelper();
            daoSession = BaseApplication.getDaoSession();
        }
        return helper;
    }

    /**
     * 加入用户
     * */
    public void addUser(List<User> users) {
        daoSession.getUserDao().insertInTx(users);
    }

    /**
     * 查询所有用户
     * */
    public List<User> searchUser() {
        return daoSession.getUserDao().loadAll();
    }

    /**
     * 根据用户名查询是否保存新用户
     * */
    public boolean isUserSaves(String UserName) {
        QueryBuilder<User> users = daoSession.getUserDao().queryBuilder();
        users.where(UserDao.Properties.UserName.eq(UserName));
        users.buildCount().count();
        return users.buildCount().count() > 0 ? true : false;
    }

//    /**
//     * 查询是否重复用户名
//     * */
//    public boolean login(String name) {
//        QueryBuilder<User> users = daoSession.getUserDao().queryBuilder();
//        users.where(UserDao.Properties.UserName.eq(name));
//        users.equals()
//        return users.buildCount().count() > 0 ? true : false;
//    }

    /**
     * 返回某个id的对象
     * */
    public User getUserByUserName(String UserName) {
        QueryBuilder<User> users = daoSession.getUserDao().queryBuilder();

        users.where(UserDao.Properties.UserName.eq(UserName));
        users.limit(1); // 只允许返回一条数据

        if (users.list() != null && users.list().size() > 0) {
            return users.list().get(0);
        }
        return null;
    }


    /**
     * 通过id删除用户
     * */
    public void clearUserList(String UserName) {
        QueryBuilder<User> users = daoSession.getUserDao().queryBuilder();
        DeleteQuery<User> user = users.where(UserDao.Properties.UserName.eq(UserName)).buildDelete();
        user.executeDeleteWithoutDetachingEntities();
    }

    /**
     * 更新用户
     * */
    public int updateUser(ContentValues values, String selection, String[] selectionArgs) {
        SQLiteDatabase db = daoSession.getUserDao().getDatabase();
        String tableName = daoSession.getUserDao().getTablename();
        return db.update(tableName, values, selection, selectionArgs);
    }

    public void updateUser(User u) {
        daoSession.getUserDao().update(u);
    }

    /**
     * 加入轮播图数据
     * */
    public void addSlide(List<com.lanou.zy.projecta.greedao.entity.SlideResult> slides) {
        daoSession.getSlideResultDao().insertOrReplaceInTx(slides);
    }

    /**
     * 查找轮播图
     * */
    public List<SlideResult> searchSlideAll() {
        return daoSession.getSlideResultDao().loadAll();
    }

    /**
     * 加入发现下一站数据
     * */
    public void addSubject (List<SubjectResult> subjects) {
        daoSession.getSubjectResultDao().insertOrReplaceInTx(subjects);
    }

    /**
     * 查找发现下一站数据
     * */
    public List<SubjectResult> searchSubjectAll() {
        return daoSession.getSubjectResultDao().loadAll();
    }

    /**
     * 首页recyclerview数据
     * */
    public void addHotNotes (List<HotNotes> hotNotes) {
        daoSession.getHotNotesDao().insertOrReplaceInTx(hotNotes);
    }

    /**
     * 查找首页recyclerview数据
     * */
    public List<HotNotes> searchHotNotesAll() {
        return daoSession.getHotNotesDao().loadAll();
    }



}
